package com.zdxlz.fcmp.upms.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdxlz.fcmp.common.core.constant.enums.OperateTypeEnum;
import com.zdxlz.fcmp.common.core.util.R;
import com.zdxlz.fcmp.common.log.annotation.LogRecord;
import com.zdxlz.fcmp.common.security.util.UserLoginInfoUtil;
import com.zdxlz.fcmp.upms.api.dto.SystemRoleDto;
import com.zdxlz.fcmp.upms.api.entity.SystemRole;
import com.zdxlz.fcmp.upms.api.service.ISystemRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import static com.zdxlz.fcmp.common.core.constant.CommonConstants.*;

/**
 * 角色信息表(SystemRole)表控制层
 *
 * @author makejava
 * @since 2025-02-24 13:49:10
 */
@RestController
@Tag(name = "角色管理模块")
@LogRecord(module = "角色管理模块")
@RequestMapping("/api/v1/sys/role")
public class SystemRoleController {

    /**
     * 服务对象
     */
    @Resource
    private ISystemRoleService systemRoleService;

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param dto 查询实体
     * @return 所有数据
     */
    @GetMapping
    @Operation(summary = "分页查询角色")
    @LogRecord(module = "分页查询角色", value = "查询条件，租户名称【{}】，角色名称【{}】，角色状态【{}】", field = {"tenantId", "name", "status"}, operType = OperateTypeEnum.SELECT)
    public R selectSystemRole(@ParameterObject Page<SystemRole> page, @ParameterObject SystemRoleDto dto) {
        return R.ok(systemRoleService.selectSystemRole(page, dto), OPERATION_SUCCESS);
    }

    /**
     * 通过ID查询 systemRole 数据
     *
     * @param id 查询实体
     * @return 所有数据
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询单个角色")
    @LogRecord(module = "查询单个角色", value = "查询条件，角色ID【{}】", field = {"id"}, operType = OperateTypeEnum.SELECT)
    public R getSystemRoleById(@PathVariable Long id) {
        return R.ok(systemRoleService.getById(id), OPERATION_SUCCESS);
    }

    /**
     * 新增数据
     *
     * @param dto 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增角色")
    @LogRecord(module = "新增角色", value = "新增角色必要条件，角色名称【{}】，角色编码：【{}】", field = {"name", "code"}, db = true, operType = OperateTypeEnum.INSERT)
    public R insertSystemRole(@RequestBody @Validated SystemRoleDto dto) {
        dto.setTenantId(UserLoginInfoUtil.getUserInfo().getTenantId());
        return R.ok(systemRoleService.insertSystemRole(dto), CREATE_SUCCESS);
    }

    /**
     * 修改数据
     *
     * @param dto 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    @LogRecord(module = "更新角色", value = "更新角色必要条件，角色ID【{}】，角色名称【{}】，角色编码：【{}】", field = {"id", "name", "code"}, db = true, operType = OperateTypeEnum.UPDATE)
    public R updateSystemRole(@PathVariable Integer id, @RequestBody @Validated SystemRoleDto dto) {
        dto.setId(id);
        dto.setTenantId(UserLoginInfoUtil.getUserInfo().getTenantId());
        return R.ok(systemRoleService.updateSystemRole(dto), UPDATE_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @Operation(summary = "删除角色")
    @LogRecord(module = "删除角色", value = "删除角色必要条件，角色ID【{}】", field = {"ids"}, db = true, operType = OperateTypeEnum.DELETE)
    public R deleteSystemRole(@RequestBody List<Integer> ids) {
        return R.ok(systemRoleService.deleteSystemRole(ids), DELETE_SUCCESS);
    }
}
